package com.sc.nft.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.admin.request.*;
import com.sc.nft.admin.vo.DaoFreeIncomeVO;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.dao.DaoDivideInfoDao;
import com.sc.nft.dao.DaoEcologyPointsTransferRecordsDao;
import com.sc.nft.dao.FilterIncomeListDao;
import com.sc.nft.dao.UserInfoDao;
import com.sc.nft.entity.DaoDivideInfo;
import com.sc.nft.entity.FilterIncomeList;
import com.sc.nft.entity.UserCreatorInfo;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.AdminSimpleDaoIdentityVO;
import com.sc.nft.entity.vo.PaageUserCreatorVO;
import com.sc.nft.entity.vo.PageDaoPointsTransferRecordsVO;
import com.sc.nft.entity.vo.SimpleDaoPageVO;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.service.UserCreatorInfoService;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import com.sc.nft.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigDecimal;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/userCreator")
@Api(tags="作者管理")
public class UserCreatorController {
    private final UserCreatorInfoService userCreatorInfoService;
    private final DaoDivideInfoDao daoDivideInfoDao;
    private final UserInfoDao userInfoDao;
    private final FilterIncomeListDao filterIncomeListDao;
    private final RedisUtil redisUtil;
    private final DaoEcologyPointsTransferRecordsDao daoEcologyPointsTransferRecordsDao;



    @PostMapping(value="/pageUserCreator", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取作者列表",notes="获取作者列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<PaageUserCreatorVO> pageUserCreator(@Valid @RequestBody UserCreatorRequest request){
       Page<UserCreatorInfo> page= userCreatorInfoService.pageUserCreator(request.getPageNo(),request.getPageSize(),request.getNickName());
        return PageResult.success( page,PaageUserCreatorVO.class);
    }

    @PostMapping(value="/addUserCreator", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "新增作者",notes="新增作者", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addUserCreator(@Valid @RequestBody AddUserCreatorRequest request){
        userCreatorInfoService.addUserCreator(request.getNickName(),request.getCreatorAvatarImg(),request.getCreatorPresentation());
        return Result.success();
    }
    @PostMapping(value="/updateUserCreator", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改作者信息",notes="修改作者信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateUserCreator(@Valid @RequestBody UpdateUserCreatorRequest request){
         userCreatorInfoService.updateUserCreator(request.getId(),request.getNickName(),
                                                  request.getCreatorPresentation(),request.getCreatorAvatarImg());
        return Result.success( );
    }

    @PostMapping(value="/deleteUserCreator", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除作者",notes="删除作者", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result deleteUserCreator(@Valid @RequestBody IdRequest request){
        new UserCreatorInfo().deleteById(request.getId());
        redisUtil.del(RedisKeyEnum.HOME_BRAND_ISLAND.getKey());
        return Result.success( );
    }


    @PostMapping(value="/getDaoDivide", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询品牌自由市场收益配置",notes="查询品牌自由市场收益配置", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<DaoFreeIncomeVO> getDaoDivide(@Valid @RequestBody IdRequest request){
        DaoDivideInfo daoDivideInfo = daoDivideInfoDao.getDivideInfoByDaoId(request.getId());
        DaoFreeIncomeVO daoFreeIncomeVO = new DaoFreeIncomeVO();
        if(ObjectUtil.isNull(daoDivideInfo)){
            daoFreeIncomeVO.setDaoId(request.getId());
            daoFreeIncomeVO.setStatus(false);
            daoFreeIncomeVO.setMintPrice(BigDecimal.ZERO);
            daoFreeIncomeVO.setShareRatio(BigDecimal.ZERO);
        }else {
            BeanUtil.copyProperties(daoDivideInfo,daoFreeIncomeVO);
            UserInfo userInfo = userInfoDao.getById(daoDivideInfo.getReturnUserId());
            daoFreeIncomeVO.setReturnUserTel(userInfoDao.getById(daoDivideInfo.getReturnUserId()).getUserTel());
        }
        return Result.success(daoFreeIncomeVO);
    }

    @PostMapping(value="/editDaoDivide", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "编辑品牌自由市场收益配置",notes="编辑品牌自由市场收益配置", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result editDaoDivide(@Valid @RequestBody EditDaoDivideRequest request){
        UserInfo userinfo = userInfoDao.getOneByUserTel(request.getReturnUserTel());
        if(ObjectUtil.isNull(userinfo)){
            return Result.fail("用户不存在");
        }
        FilterIncomeList filterUser = filterIncomeListDao.getIsTeamIncomeByUserId(userinfo.getId());
        if(ObjectUtil.isNull(filterUser)||filterUser.getDaoId().longValue()!=request.getDaoId().longValue()){
            return Result.fail("用户不存在");
        }
        DaoDivideInfo daoDivideInfo = daoDivideInfoDao.getDivideInfoByDaoId(request.getDaoId());

        if(ObjectUtil.isNull(daoDivideInfo)){
            daoDivideInfo = new DaoDivideInfo();
            daoDivideInfo.setDaoId(request.getDaoId());
            daoDivideInfo.setMintPrice(request.getMintPrice());
            daoDivideInfo.setShareRatio(request.getShareRatio());
            daoDivideInfo.setReturnUserId(userinfo.getId());
            daoDivideInfo.setStatus(request.getStatus());
            daoDivideInfo.insert();
        }else {
            daoDivideInfo.setDaoId(request.getDaoId());
            daoDivideInfo.setMintPrice(request.getMintPrice());
            daoDivideInfo.setShareRatio(request.getShareRatio());
            daoDivideInfo.setReturnUserId(userinfo.getId());
            daoDivideInfo.setStatus(request.getStatus());
            daoDivideInfo.updateById();
        }
        return Result.success();
    }


    @PostMapping(value="/getSimpleDaoPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取简单的dao列表",notes="获取简单的dao列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<SimpleDaoPageVO> getSimpleDaoPage(@Valid @RequestBody SimpleDaoPageRequest request){
        return PageResult.success(userCreatorInfoService.getSimpleDaoPage(request.getDaoName(),request.getPageNo(),request.getPageSize()));
    }

    @PostMapping(value="/getSimpleOnlineTimeDaoPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取所有dao的简单列表(配置上线时间)",notes="获取所有dao的简单列表(配置上线时间)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<SimpleDaoPageVO> getSimpleOnlineTimeDaoPage(@Valid @RequestBody SimpleDaoPageRequest request){
        return PageResult.success(userCreatorInfoService.getSimpleOnlineTimeDaoPage(request.getDaoName(),request.getPageNo(),request.getPageSize()));
    }

    @PostMapping(value="/getSimpleDaoIdentityPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取简单的dao身份列表",notes="获取简单的dao身份列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AdminSimpleDaoIdentityVO> getSimpleDaoIdentityPage(@Valid @RequestBody SimpleDaoIdentityPageRequest request){
        return PageResult.success(userCreatorInfoService.getSimpleDaoIdentityPage(request.getDaoId(),request.getCollectionName(),request.getIdentityName(),request.getPageNo(),request.getPageSize()));
    }


    @PostMapping(value="/getPageDaoEcologyPointsTransferRecords", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "企业燃料转增市值列表",notes="企业燃料转增市值列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<PageDaoPointsTransferRecordsVO> getPageDaoEcologyPointsTransferRecords(@Valid @RequestBody GetPageDaoEcologyPointsTransferRecordsRequest request){
        return PageResult.success(daoEcologyPointsTransferRecordsDao.getPageDaoPointsTransferRecords(request.getDaoId(),request.getUserTel(),request.getStartTime(),request.getEndTime(),request.getPageNo(),request.getPageSize()));
    }


}
